package com.xk857.simpleeffect.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.param.UpdateFriendNickNameParam;
import com.xk857.simpleeffect.domain.vo.FriendVo;
import com.xk857.simpleeffect.enums.RequestEnum;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.button.roundbutton.RoundButton;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.lang.reflect.Type;
import java.util.function.Consumer;

public class UpdateFriendNickNameActivity extends AppCompatActivity {

    private TitleBar titleBar;
    private EditText editText;
    private RoundButton finishButton;
    private long customerId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.white));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_update_friend_nick_name);
        sendInfo();
        initView();
    }

    private void initView() {
        titleBar = findViewById(R.id.title_bar);
        titleBar.setLeftClickListener(v -> finish());
        editText = findViewById(R.id.editText);
        finishButton = findViewById(R.id.finish_button);
        finishButton.setOnClickListener(v -> {
            UpdateFriendNickNameParam param = new UpdateFriendNickNameParam(customerId, editText.getText().toString());
            Type type = new TypeToken<Result<Boolean>>() {}.getType();
            OkHttpUtils.getInstance().put(this, RequestEnum.UPDATE_FRIEND_NICK_NAME, type, param, true, (Consumer<Boolean>) b -> {
                runOnUiThread(() -> {
                    if (b) {
                        new MaterialDialog.Builder(this)
                                .iconRes(R.drawable.icon_success)
                                .title(R.string.success)
                                .content("昵称修改成功")
                                .positiveText(R.string.yes)
                                .onPositive((dialog, which) -> finish())
                                .show();
                    }
                });
            });
        });
    }

    private void sendInfo() {
        customerId = getIntent().getLongExtra("customerId", -1);
        String url = OkHttpUtils.BASE_URL + "/api/v1/friendship/friend/info/" + customerId;
        Type type = new TypeToken<Result<FriendVo>>() {}.getType();
        OkHttpUtils.getInstance().get(this, url, type, false, (Consumer<FriendVo>) friend -> {
            runOnUiThread(() -> {
                editText.setText(friend.getRealNickName());
            });
        });
    }
}